<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-8-1
 * Time: 下午3:10
 */
namespace API;

use DB\Article;
use DB\ArticleInfo;
use Tiny\Log;
use Tiny\Logger;
use Tiny\Request;
use Tiny\Response;
use Tiny\ServiceAPI;
use Tiny\ServiceRequestData;
use Tiny\ServiceResponseData;

class GetHotArticlesRequestData extends ServiceRequestData{
  public $limit;
}

class GetHotArticlesResponseItem{
  public $id;
  public $title;

  public function __construct(string $id,string $title) {
    $this->id = $id;
    $this->title = $title;
  }
}

class GetHotArticlesResponseData extends ServiceResponseData{
  public $result;
  public $debug = [];
}

class GetHotArticles extends ServiceAPI{
  protected function run(Request $request, Response $response) {
    /**
     * @var $rqData GetHotArticlesRequestData
     */
    $rqData = $request->data;
    $article = new Article();
    $filter = [];
    $options = ["limit" => $rqData->limit ?? 10];
    $list = $article->getMany($filter,$options);
    $response->data = new GetHotArticlesResponseData();
    foreach ($list as $item){

      /**
       * @var $item ArticleInfo
       */
      $response->data->result[] = new GetHotArticlesResponseItem($item->_id,$item->title);
    }
  }
}